package excercise.Page28;

/**
 * @Author: Miyako
 * @Date: 2024-07-15-9:16
 * @Description:画一个圆
 */
public class Circle extends Shape {
    static final double pai;//常量pai无法改变

    static {
        pai = 3.1415926;
    }

    double r;

    Circle() {
    }

    /**
     * 用构造参数传入半径与xy的值
     */
    Circle(double x) {
        this.r = x / 2;
        setX(x);
        setY(x);
    }
    /**
     * 打印圆
     */
    @Override
    void print() {
        for (int y = 0; y < super.getY(); y++) {
            for (int x = 0; x < super.getX(); x++) {
                // 计算相对于圆心的坐标
                int centerX = (int) super.getX() / 2;
                int centerY = (int) super.getY() / 2;
                // 判断该点是否在圆上
                if (Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2) <= Math.pow(r, 2)) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println(); // 换行
        }
    }

    /**
     * 周长
     */
    double perimeter() {
        return super.perimeter = r * pai * 2;
    }

    /**
     * 面积
     *
     * @return
     */
    @Override
    double area() {
        return super.area = r * r * pai;
    }
}
